package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.entity.*;
import com.service.*;
import com.util.VeDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController // 定义为控制器 返回JSON类型数据
@RequestMapping(value = "/chart", produces = "application/json; charset=utf-8") // 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class ChartController extends BaseController {

    // TODO Auto-generated method stub

    @Autowired // @Autowired的作用是自动注入依赖的ServiceBean
    private UsersService usersService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private CateService cateService;
    @Autowired
    private VisitService visitService;

    @PostMapping("chartUsers.action") // 定义访问方法路径
    public Map<String, Object> chartUsers(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        String start = obj.getString("start");
        String end = obj.getString("end");
        System.out.println("start ==> " + start);
        System.out.println("end ==> " + end);
        long days = VeDate.getDays(end, start) + 1;
        JSONArray total = new JSONArray();//
        JSONArray day = new JSONArray(); // 存放名称
        for (int i = 0; i < days; i++) {
            String nxtDay = VeDate.getNextDay(start, "" + i);
            Users users = new Users();
            users.setRegdate(nxtDay);
            List<Users> list = this.usersService.getUsersByCond(users);
            total.add(list.size());
            day.add(nxtDay);
        }
        map.put("total", total.toString());
        map.put("days", day.toString().replaceAll("\"", ""));
        return map;
    }

    @PostMapping("chartOrders.action") // 定义访问方法路径
    public Map<String, Object> chartOrders(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        String start = obj.getString("start");
        String end = obj.getString("end");
        long days = VeDate.getDays(end, start) + 1;
        JSONArray total = new JSONArray();//
        JSONArray day = new JSONArray(); // 存放名称
        for (int i = 0; i < days; i++) {
            String nxtDay = VeDate.getNextDay(start, "" + i);
            Orders orders = new Orders();
            orders.setAddtime(nxtDay);
            List<Orders> ordersList = this.ordersService.getOrdersByCond(orders);
            double num = 0;
            for (Orders x : ordersList) {
                num += Double.parseDouble(x.getTotal());
            }
            total.add(VeDate.getDouble(num));
            day.add(nxtDay);
        }
        map.put("total", total.toString());
        map.put("days", day.toString().replaceAll("\"", ""));
        return map;
    }

    @PostMapping("chartGoods.action") // 定义访问方法路径
    public Map<String, Object> chartGoods() {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONArray names = new JSONArray();
        JSONArray count = new JSONArray();// 定义count存放数值
        List<Cate> cateList = this.cateService.getAllCate();
        for (Cate c : cateList) {
            names.add(c.getCatename());
            Goods goods = new Goods();
            goods.setCateid(c.getCateid());
            List<Goods> goodsList = this.goodsService.getGoodsByCond(goods);
            int num = 0;
            for (Goods g : goodsList) {
                num += Integer.parseInt(g.getSellnum());
            }
            count.add(num);
        }
        map.put("count", count.toString());
        map.put("names", names.toString().replaceAll("\"", ""));
        return map;
    }

    @PostMapping("chartTopic.action") // 定义访问方法路径
    public Map<String, Object> chartTopic() {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONArray names = new JSONArray();
        JSONArray count = new JSONArray();// 定义count存放数值
        int stars1 = 0;
        int stars2 = 0;
        int stars3 = 0;
        int stars4 = 0;
        int stars5 = 0;
        names.add("非常满意");
        names.add("满意");
        names.add("一般");
        names.add("不满意");
        names.add("非常不满意");
        List<Topic> topicList = this.topicService.getAllTopic();
        for (Topic topic : topicList) {
            if ("1".equals(topic.getNum())) {
                stars1++;
            }
            if ("2".equals(topic.getNum())) {
                stars2++;
            }
            if ("3".equals(topic.getNum())) {
                stars3++;
            }
            if ("4".equals(topic.getNum())) {
                stars4++;
            }
            if ("5".equals(topic.getNum())) {
                stars5++;
            }
        }
        count.add(stars5);
        count.add(stars4);
        count.add(stars3);
        count.add(stars2);
        count.add(stars1);
        map.put("count", count.toString());
        map.put("names", names.toString().replaceAll("\"", ""));
        return map;
    }

    //统计日访问次数
    @PostMapping("chartDayVisit.action") // 定义访问方法路径
    public Map<String, Object> chartDayVisit(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        String start = obj.getString("start");
        String end = obj.getString("end");
        System.out.println("start ==> " + start);
        System.out.println("end ==> " + end);
        long days = VeDate.getDays(end, start) + 1;
        JSONArray total = new JSONArray();//
        JSONArray day = new JSONArray(); // 存放名称
        for (int i = 0; i < days; i++) {
            String nxtDay = VeDate.getNextDay(start, "" + i);
            Visit visit = new Visit();
            visit.setDays(nxtDay);
            List<Visit> list = this.visitService.getVisitByCond(visit);
            if (null == list || 0 == list.size()) {
                total.add(0);
            } else {
                total.add(list.get(0).getNum());
            }
            day.add(nxtDay);
        }
        map.put("total", total.toString());
        map.put("days", day.toString().replaceAll("\"", ""));
        return map;
    }

    // TODO Auto-generated method stub
}
